package com.platform.oss.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.oss.entity.Oss;
import com.platform.oss.entity.vo.OssQuery;
import com.platform.oss.service.OssService;
import com.platform.common_utils.R;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/oss")
@CrossOrigin
public class OssController {

    @Autowired
    private OssService ossService;

    @ApiOperation("文件上传")
    @PostMapping("/upload/{uploader}/{manager}")
    public R uploadOssFile(MultipartFile file,
                           @PathVariable String uploader,
                           @PathVariable String manager) {
        //获取上传文件，MultipartFile
        //返回上传oss的url
        String filename = file.getOriginalFilename();
        Oss oss = new Oss(filename, uploader, manager);
        boolean result = ossService.save(oss);
        ossService.uploadFileAvatar(file);
        if (result) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    

   //分页查询文件数据
    @ApiOperation(value = "分页查询文件数据")
    @GetMapping("pageOss/{current}/{size}")
    public R pageOss(@PathVariable Long current,
                         @PathVariable Long size) {
        //创建page
        Page<Oss> pageOss = new Page<>(current, size);
        //调用方法实现查询
        ossService.page(pageOss, null);

        List<Oss> records = pageOss.getRecords();
        long total = pageOss.getTotal();
        return R.ok().data("total", total).data("records", records);
    }

   //条件查询带分页文件数据
    @ApiOperation(value = "条件查询带分页文件数据")
    @PostMapping("pageOssCondition/{current}/{size}")
    public R pageOssCondition(@PathVariable Long current,
                              @PathVariable Long size,
                              @RequestBody(required = false) OssQuery ossQuery) {
        //创建page
        Page<Oss> pageOss = new Page<>(current, size);
        //构建条件
        QueryWrapper<Oss> wrapper = new QueryWrapper<>();
        //多条件查询
        String filename = ossQuery.getFilename();
        String TimeBegin = ossQuery.getTimeBegin();
        String TimeEnd = ossQuery.getTimeEnd();
        String uploader = ossQuery.getUploader();
        if(!StringUtils.isEmpty(uploader)) {
            wrapper.eq("uploader",uploader);
        }
        if(!StringUtils.isEmpty(filename)) {
             wrapper.like("filename",filename);
        }
        if(!StringUtils.isEmpty(TimeBegin)) {
            wrapper.ge("fileCreate",TimeBegin);
        }
        if(!StringUtils.isEmpty(TimeEnd)) {
            wrapper.le("fileCreate",TimeEnd);
        }

        //调用方法实现查询
        ossService.page(pageOss,wrapper);

        long total = pageOss.getTotal();//获取总记录数
        List<Oss> records = pageOss.getRecords();//获取分页后的list集合
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    //删除文件
    @ApiOperation(value="根据id删除文件")
    @DeleteMapping("delete/{id}")
    public R deleteChapter(@PathVariable int id) {

        Oss one = ossService.getById(id);
        String filename = one.getFilename();
        boolean result = ossService.removeById(id);
        ossService.DeleteFile(filename);
        if (result) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    //文件下载
    @ApiModelProperty(value = "根据文件名下载")
    @GetMapping("file/download/{filename}")
    public void download(@PathVariable String filename, HttpServletResponse response) throws IOException {
        //通知浏览器以附件形式下载
        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String(filename.getBytes(), "ISO-8859-1"));
      ossService.downloadfile(response.getOutputStream(), filename);
    }

}





